package com.leyou.auth.config;

import com.leyou.auth.interceptors.LoginInterceptor;
import com.leyou.auth.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
@ConditionalOnProperty(prefix = "ly.auth",name = "enable",havingValue = "true")
public class MvcConfig implements WebMvcConfigurer {

    @Autowired
    @Lazy  //懒加载
    private JwtUtils jwtUtils;
    @Autowired
    private ClientProperties prop;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor(jwtUtils));
        //添加拦截路径
        List<String> includeFilterPaths = prop.getIncludeFilterPaths();
        if (!CollectionUtils.isEmpty(includeFilterPaths)){
            registration.addPathPatterns(includeFilterPaths);
        }
        //添加排除路径
        List<String> excludeFilterPaths = prop.getExcludeFilterPaths();
        if (!CollectionUtils.isEmpty(excludeFilterPaths)){
            registration.excludePathPatterns(excludeFilterPaths);
        }
    }


}
